package com.ctshk.rpc.train.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.SysAgentQueryListDTO;
import com.ctshk.rpc.system.dto.SysAgentUsableDTO;
import com.ctshk.rpc.system.req.SysAgentQueryListReq;
import com.ctshk.rpc.system.req.SysAgentQueryUsableListReq;
import com.ctshk.rpc.system.service.ISysAgentService;
import com.ctshk.rpc.train.dto.Constants;
import com.ctshk.rpc.train.dto.TrainSysAgentQueryListDTO;
import com.ctshk.rpc.train.entity.TrainProductChannelOfflineAgentCustom;
import com.ctshk.rpc.train.entity.TrainProductStrategy;
import com.ctshk.rpc.train.mapper.TrainProductChannelOfflineAgentCustomMapper;
import com.ctshk.rpc.train.mapper.TrainProductStrategyMapper;
import com.ctshk.rpc.train.req.TrainSysAgentQueryListReq;
import com.ctshk.rpc.train.service.ITrainProductChannelOfflineAgentCustomService;
import com.ctshk.rpc.train.service.ITrainProductStrategyService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 火车票产品---线下代理商自定义用户部门设置 服务实现类
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-03-15
 */
@DubboService
public class TrainProductChannelOfflineAgentCustomServiceImpl extends ServiceImpl<TrainProductChannelOfflineAgentCustomMapper, TrainProductChannelOfflineAgentCustom> implements ITrainProductChannelOfflineAgentCustomService {

    @DubboReference
    private ISysAgentService sysAgentService;

    @Resource
    private TrainProductStrategyMapper trainProductStrategyMapper;

    @Override
    public PageResponse<TrainSysAgentQueryListDTO> queryAgentList(TrainSysAgentQueryListReq req, TokenUser tokenUser) {
        TrainProductStrategy trainProductStrategy = trainProductStrategyMapper.selectById(req.getIncreaseStrategyId());
        if(Constants.SALE_CHANNEL_TYPE_ALL.equals(trainProductStrategy.getAgentSalePermissionType())) {
            // 销售渠道类型：所有
        } else if((Constants.SALE_CHANNEL_TYPE_SELECTED.equals(trainProductStrategy.getAgentSalePermissionType()))) {
            // 销售渠道类型：选中的
            List<TrainProductChannelOfflineAgentCustom> trainProductChannelOfflineAgentCustoms = baseMapper.selectList(new QueryWrapper<TrainProductChannelOfflineAgentCustom>().lambda().eq(TrainProductChannelOfflineAgentCustom::getIncreaseStrategyId, req.getIncreaseStrategyId()
            ));
            List<Long> idList = trainProductChannelOfflineAgentCustoms.stream().map(TrainProductChannelOfflineAgentCustom::getAgentId).collect(Collectors.toList());
            req.setIdList(idList);
        } else {
            //销售渠道类型：禁售
            return PageResponse.empty(1);
        }

        SysAgentQueryUsableListReq copy = EntityUtil.copy(req, SysAgentQueryUsableListReq.class);
        Result<List<SysAgentUsableDTO>> listResult = sysAgentService.queryUsableAgentList(copy);
        List<SysAgentUsableDTO> records = listResult.getData();
        IPage<TrainSysAgentQueryListDTO> agentPage = new Page<>(req.getPageNo(), req.getPageSize());
        agentPage.setRecords(EntityUtil.copyList(records,TrainSysAgentQueryListDTO.class));
        return new PageResponse<>(agentPage.getRecords(), agentPage.getCurrent() == 1,
                agentPage.getTotal() <= (agentPage.getCurrent() * agentPage.getSize()),
                agentPage.getTotal(), agentPage.getCurrent(), agentPage.getSize());
    }
}
